var app = getApp()

var QQMapWX = require('../../../../utils/qqmap-wx-jssdk.min.js')

const wxMap = new QQMapWX({
  key: 'NSUBZ-2GEKP-AYYDK-LMZXC-KE2LO-46B6H'
})
Page({
    data: {
      address: '',
      pictures: [],
      bookName: '',
      latitude: '',
      longitude: '',
      trentInfo: '',
      bookInfoId: ''
      // friendId: [],
      // friendName: []
    },

    //打开添加书籍页面
    addBooks () {
      wx.navigateTo({
        url: '/package/my/pages/addBooks/addBooks',
      })
    },

    // 打开提醒谁看页面
    remind () {
      wx.navigateTo({
        url: '/package/my/pages/remind/remind',
      })
    },


  changeImg() {
    var _this = this
    wx.chooseImage({
      count: 9 - _this.data.pictures.length,
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {


        console.log(res.tempFiles)

          // console.log(res)
          for (var i = 0; i< res.tempFilePaths.length;i++) {

            
            // _this.getImageyanzheng(res.tempFilePaths[i])
            _this.upload(res.tempFilePaths[i])
          }
      }
    })
  },

  // getFile (fileurl) {

  //    wx.uploadFile({
       
  //    })
  // },


  


  upload(url) {

    wx.showLoading({
      title: '上传中',
      mask: true
    })
    var _this = this
    app.uploadFile('img', url, 'shuDaixiong(v1.0)/otherImg/', function (result) {
      console.log(result)
      wx.hideLoading()
      _this.getImageyanzheng(result)
      // _this.getImgs(result)
    })
  },


  getImgs (url) {
    var list = this.data.pictures

    list.push(url)

    this.setData({
      pictures: list
    })
  },



  bindtrentInfo (e) {
    // console.log(e)
    var trentInfo = e.detail.value

    if (trentInfo.length > 256) {
      wx.showToast({
        title: '文字已上限',
        icon: 'none'
      })
    }

    this.setData({
      trentInfo: trentInfo
    })
  },


  //地址选择

  chooseDizhi () {
    var _this = this
    wx.chooseLocation({
      success: function (res) {
        console.log(res)

        _this.setData({
          address: res.name,
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })
  },


  //删除图片

  cutImage (e) {
    console.log(e)


    var url = e.currentTarget.dataset.url

    for (var i = 0;i< this.data.pictures.length;i++) {
      if (url == this.data.pictures[i]) {
        var list = this.data.pictures
        list.splice(i,1)

        this.setData({
          pictures: list
        })
      }
    }
  },


  //书籍添加
  addName2 (bookname,bookid) {
    this.setData({
      bookName: bookname,
      bookInfoId: bookid
    })
  },


  //验证图片接口

  getImageyanzheng (imgurl) {

    var _this = this

    var params = {
      multipartFile: imgurl
    }
    wx.request({
      url: "https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxTrends/picCheck/auth",
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res)=>{
        if (res.data.code == 200) {
          if (res.data.data.isCheck==0) {
            _this.getImgs(imgurl)

            // _this.getContentyanzheng(content)
          }else{
            wx.showToast({
              title: res.data.data.checkName,
              icon: 'none'
            })
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
    })
  },

  //验证内容接口
  getContentyanzheng (content) {


    var _this = this
    var params = {
      content: content
    }
    wx.request({
      url: "https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxTrends/contentCheck/auth",
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {

          if (res.data.data.isCheck == 0) {

            var params2 = {
              address: _this.data.address,
              pictures: _this.data.pictures.length == 1 ? this.data.pictures[0] : _this.data.pictures.join(','),
              bookName: _this.data.bookName,
              latitude: _this.data.latitude,
              longitude: _this.data.longitude,
              trentInfo: _this.data.trentInfo,
              bookInfoId: _this.data.bookInfoId
            }


            _this.fabuApi(params2)
          }else{
            wx.showToast({
              title: res.data.data.checkName,
              icon: 'none'
            })
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
        }
      }
      
    })
  },
  //动态发布按钮
  paySuccess () {

    var _this = this
    // console.log(this.data.pictures)

    if (this.data.pictures.length == 0) {
      wx.showToast({
        title: '请添加图片',
        icon: 'none'
      })
      return
    }
    if (this.data.trentInfo == '') {
      wx.showToast({
        title: '请添加正文',
        icon: 'none'
      })
      return
    }

    this.getContentyanzheng(this.data.trentInfo)


    

    // this.getImageyanzheng(this.data.trentInfo)


    // this.getImageyanzheng(this.data.pictures, this.data.trentInfo)


    // return



    // if (this.data.bookName=='') {
    //   wx.showToast({
    //     title: '请添加书籍',
    //     icon: 'none'
    //   })
    //   return

    // }
    // if (this.data.address == '') {
    //   wx.showToast({
    //     title: '请添加地址',
    //     icon: 'none'
    //   })
    //   return
    // }

    // var params = {
    //   address: this.data.address,
    //   pictures: this.data.pictures.length == 1 ? this.data.pictures[0]: this.data.pictures.join(','),
    //   bookName: this.data.bookName,
    //   latitude: this.data.latitude,
    //   longitude: this.data.longitude,
    //   trentInfo: this.data.trentInfo,
    //   bookInfoId: this.data.bookInfoId
    // }


    // _this.fabuApi(params)


    // wx.cloud.init()

    // wx.cloud.callFunction({
    //   name: 'msgcheck',
    //   name: 'imgcheck',
    //   data: {
    //     content: params.trentInfo,
    //     imgcontent: params.pictures
    //   }
    // }).then(res =>{
    //   console.log(res)
    //   if (res.errMsg == "cloud.callFunction:ok") {
        
    //   }else{
    //     wx.hideLoading();
    //     wx.showModal({
    //       title: '提醒',
    //       content: '请注意言论',
    //       showCancel: false
    //     })
    //   }
    // })

    // console.log(params)
    // this.fabuApi(params)
  },



  fabuApi (params) {

    wx.request({
      url: 'https://devapi.xiaoshitimebank.com/user/sdx/sdx/tSdxTrends/mod/auth',
      data: params,
      method: 'POST',
      header: {
        "content-type": "application/x-www-form-urlencoded; charset=UTF-8",
        "token": wx.getStorageSync('token'),
        "uid": wx.getStorageSync('uuid')
      },
      success: (res) => {
        if (res.data.code == 200) {
          wx.showToast({
            title: '发布成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function (){

         
          wx.navigateTo({
            url: '/package/my/pages/myTrends/myTrends',
          })

          },2000)
          // setTimeout(function () {
          //   wx.navigateBack()
          // },2000)
        }
      }
    })
  },



  //亲空地址
  closeAddress () {
    this.setData({
      address: ''
    })
  },
  //清空书籍
  closeName () {
    this.setData({
      bookName: '',
      bookInfoId: ''
    })
  }
})
